﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;
using MockPrjTeam1.DAO;

namespace MockPrjTeam1.Biz
{
    public class ChangePassBiz
    {

        public static bool IfPassChanged(string UserName, string Email)
        {
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            var random = new Random();
            var result = new string(
                Enumerable.Repeat(chars, 8)
                          .Select(s => s[random.Next(s.Length)])
                          .ToArray());
            var DAO = new ChangePassDAO();
            
            if (DAO.UpdateUser(UserName,Email,result))
            {


                MailMessage mailMessage = new MailMessage();
                mailMessage.To.Add(Email);
                mailMessage.Subject = "Thay đổi mật khẩu";
                mailMessage.Body = "<html><body>Mật khẩu mới của bạn là  "+result+"</body></html>";
                mailMessage.IsBodyHtml = true;
                mailMessage.From = new MailAddress("life4dieth2@gmail.com");
 
                SmtpClient client = new SmtpClient();  
                

                client.Credentials = new NetworkCredential("life4dieth2@gmail.com", "lala1234");
                client.Host = "smtp.gmail.com";
                 client.Port = 587;           
                 client.EnableSsl = true;
                
                 client.Send(mailMessage);

                return true;
            }
            return false;
        }
    }
}